
const app = getApp();
const req = require("../../utils/http.js");
Page({
  data: {
    BackgroundImg: 'https://syl-estate.oss-cn-shenzhen.aliyuncs.com/template/645505222748631502.jpg',
    userInfo:'',
    loginShow:false,//弹出登录页
    popPhone:false,
    logText: true,
    setHidden: '',
    code:'获取验证码',
    disabled:false,
    usePhone:'',
    isGet: true,
    sec:30,
    Discount:'',
    member: false, 
    details: [
    
      { id: '1', text: '专属导购', img: '/imgs/user/dg.png' },
      { id: '2', text: '收获地址', img: '/imgs/user/sz.png' },
      { id: '3', text: '会员充值', img: '/imgs/user/cz.png' },
      { id: '4', text: '消费记录', img: '/imgs/user/pt.png' },
      ]
  },


  userLogin(){//弹出登录头像
    this.setData({
      loginShow:true
    })
  },
  //头像授权
  bindGetUserInfo: function (e) {
    let t = this
    if (e.detail.userInfo) {
      if (t.data.member==true){ //获取手机号头像授权后自动弹出授权手机号
        t.setData({
          popPhone:true
        })
      }
      t.setData({
        loginShow: false,
        userInfo: e.detail.userInfo,
      })
      let params = {
        encryptedData: e.detail.encryptedData,
        ivStr: e.detail.iv
      }
      req._objPost('/mini/saveUserinfo', "POST", params).then(res => {
        if(res.data.code==!200){
          wx.showToast({
            title: res.data.message,
            icon:'none',
            duration:1000
          })
        }
      })
     
    } else {
      wx.showToast({
        title: "为了您更好的体验,请先同意授权",
        icon: 'none',
        duration: 2000
      });
    }
  },
  // ---点击未登录按钮---
  notLanding() {
    this.setData({
      loginShow: false  //授权手机号隐藏
    })
    wx.navigateTo({
      url: '/pages/index/index',
    })
  },
  //内容列表
  contentClike(e){
    let id = e.currentTarget.dataset.id
    if (!this.data.mobile){
      wx.showToast({
        title: '请领取会员卡',
        icon: 'none',
        duration: 1000
      })
      return
    }
    if(id==1){
      wx.showToast({
        title: '暂无数据...',
        icon:'none',
        
      })
    }
    if(id==2){//收获地址
      wx.navigateTo({
        url: '../address/index',
      })
    }
    if(id==3){
      wx.navigateTo({
        url: '../recharge/index',
      })
    }
    if(id==4){ //消费记录
    let type = '2'
      wx.navigateTo({
        url: '../balance/balance?type='+ type,
      })
    }
  },

  balanceClik(e){//余额 积分
    if (this.data.mobile){
      let type = e.currentTarget.dataset.type
      wx.navigateTo({
        url: '../balance/balance?type=' + type,
      })
    }else{
      wx.showToast({
        title: '请领取会员卡',
        icon: 'none',
        duration:1000
      })
    }
   
 

  },
  // _-手机号授权--
  getPhoneNumber: function (e) {
    let t = this
    t.setData({
      popPhone: false
    })
    if (e.detail.iv) {
      const params = {
        encryptedData: e.detail.encryptedData,
        ivStr: e.detail.iv,
      }
      req._objPost("/mini/savePhone",'POST',params).then(res => {
        if (res.data.code == 200) {
          wx.showToast({
            title: res.data.message,
            icon:'none',
            duration:1500
          })
          setTimeout(()=>{
            t.isUserinfo()
          },500)
          t.setData({
            popPhone:false,
            logText:false,
          })

        }else{
          wx.showToast({
            title: res.message,
            icon: 'none',
            duration: 1500
          })
          }
      })
    } else {
      console.log("用户按了拒绝按钮");
      wx.showToast({
        title: "为了您更好的体验,请先同意授权",
        icon: 'none',
        duration: 1600,  
      });  
    }
  },

  closeDetailMode() {
    this.setData({
      popPhone: false
    })
  },


  //-----获取 ----验证码------
  getCode() {
    const t = this;
    var usePhone = t.data.usePhone; //用户输入的手机号
    console.log(usePhone)
    let str = /^1\d{10}$/;
    if (!str.test(usePhone)) {
      wx.showToast({
        title: '手机号格式不正确',
        icon: 'none'
      })
      return
    }
    let params = {
      phone: usePhone
    }
    let isGet = t.data.isGet
    if (isGet==true){
      req.wxReq('/api/face/getValidateCode', "GET", params).then(res => {
        console.log('短信验证', res)
        wx.showToast({
          title: res.data.message,
          icon: 'none'
        })
        t.setData({
          isGet: false,
          phone: usePhone
        });
        if (res.data.code == 200) {
          var remain = 30;
          var time = setInterval(function () {
            if (remain == 1) {
              clearInterval(time)
              t.setData({
                sec: 30,
                isGet: true
              })
              return false
            }
            remain--;
            t.setData({
              sec: remain
            })
          }, 1000)
          
        }
        else {
          t.setData({
            isGet: true
          })
        }

      })
    }
    
    

  },


  // 领卡 会员登录（获取用户手机号）
  memberClik(){
    let t = this
    let userInfo = t.data.userInfo

    if (userInfo){
      t.setData({
        popPhone: true
      })
    }else{
      t.setData({
        loginShow:true,
        member:true// 弹出手机号
      })
    }
   
  },

  // 查询当前用户
  isUserinfo(){
    let t = this
    let params={}
    req.wxReq('/mini/isUserinfo',"GET",params).then(res=>{
      if(res.data.code==200){
        console.log('isUserinfo',res)
        wx.setStorageSync('isUserinfo', res.data.result)
        let mobile = res.data.result.mobile
        let Discount = ''
        if (res.data.result.memberLevel){
          if (res.data.result.memberLevel.discount == 0 || res.data.result.memberLevel == 1) {
            Discount = '暂无折扣'
          } else {
            let price = res.data.result.memberLevel.discount * 10
            Discount = price + '折'
          }
        }
        t.setData({
          userInfo: res.data.result,
          loginShow:false,
          Discount,
          mobile,
        })
      }
 
  
    })
  },

   //获取手机号验证码
  userNamePhone(e) { 
    console.log(e)
    const t = this
    var val = e.detail.value;
    var name = e.target.id
    var maps = {}
    maps[name] = val
    t.setData(maps)
    console.log('00', t.data.usePhone)
  },
  
  
  // 手动输入手机号
  clikGroup() {
    let t = this
   t.setData({
     GroupShow:true
   })
  },

   //提交
  confirm(){
    let t = this
    let usePhone = t.data.usePhone
    let useCaptcha = t.data.useCaptcha
    let params = {
      phone: usePhone,
      validateCode: useCaptcha
    }
    req._objPost('/mini/savePhone',"POST",params).then(res=>{
      console.log(res)
      if(res.data.code==200){
      
        t.setData({
          GroupShow:false
        })
      }else{
        wx.showToast({
          title: res.data.message,
          duration: 1000,
          icon: 'none'
        })
      }
    })
  },
  
  abolish(){
    this.setData({
      GroupShow: false
    })
  },
 
 

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let t = this
    let url = app.globalData.baseUrl
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        if (res.code) {
          wx.request({
            url: url + '/mini/login',
            method: 'GET',
            data: { code: res.code },
            header: {
              'content-type': 'application/json'
            },
            success: res => {
              console.log('1',res)
              if (res.data.code == 200) {
                 wx.setStorageSync('token', res.data.result.token);
                // this.globalData.token = res.data.result.token
                t.isUserinfo()
              }
            },
          })
        } else {
          console.log('获取用户登录态失败！' + res.errMsg);
        }
      }
    })

    wx.setNavigationBarTitle({
      title: '会员管理',
    })

    //隐藏tabbar
    wx.hideTabBar()

  },
 
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log('111')
    // 显示动态标题动画
    wx.showNavigationBarLoading();
    this.isUserinfo()
    //取消标题栏进度条效果
    setTimeout(()=>{
      wx.hideNavigationBarLoading()
      //取消页面刷新动画
      wx.stopPullDownRefresh()
    },600)
    
  },

 
})
